package com.badibadi.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;

import com.badibadi.activity.RegionHomePageActivity;
import com.badibadi.infos.AreaListModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.libjph.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class XListView1Adapter_4 extends BaseAdapter {
	private Context context;
	private List<AreaListModel> areaListModels;
	private DisplayImageOptions options;
	private String img="http://photo.uniclubber.com/Public/Uploads/2014-10/141416150316618.jpg!imgshow";

	public XListView1Adapter_4(Context context,
			List<AreaListModel> areaListModels) {
		this.context = context;
		this.areaListModels = areaListModels;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return areaListModels.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return areaListModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
		convertView = inflater.inflate(R.layout.fragment_index_club3_1, null);
		ImageView area_list_item_img = (ImageView) convertView
				.findViewById(R.id.area_list_item_img);
		TextView area_list_item_name = (TextView) convertView
				.findViewById(R.id.area_list_item_name);
		TextView area_list_item_num = (TextView) convertView
				.findViewById(R.id.area_list_item_num);
		TextView area_list_item_details = (TextView) convertView
				.findViewById(R.id.area_list_item_details);
		try {
			area_list_item_name.setText(areaListModels.get(position)
					.getArea_name());
			area_list_item_num.setText(""
					+ areaListModels.get(position).getClubCount());
			if (!StringUtil.isNullOrEmpty(areaListModels.get(position)
					.getArea_img())) {
				ImageLoader.getInstance().displayImage(
						Constants.BadiDownImgUrl
								+ areaListModels.get(position).getArea_img()
								+ Constants.appPhoto4img, area_list_item_img,
						options);
			} else {
				ImageLoader.getInstance().displayImage(img, area_list_item_img,
						options);
			}

			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (areaListModels.get(position).getArea_id() != null
							&& !areaListModels.get(position).getArea_id()
									.equals("null")) {
						Intent intent = new Intent(context,
								RegionHomePageActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra("area_id", areaListModels.get(position)
								.getArea_id());
						context.startActivity(intent);
					} else {
						Toast.makeText(
								context,
								context.getResources().getString(
										R.string.l_xb167), 0).show();
					}
				}
			});
		} catch (Exception e) {
			   
		}

		return convertView;
	}
}
